package com.edgardeng.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 3 个交换机  4个队列，4 个消费者
 *
 */
@Configuration
@PropertySource("classpath:rabbitmq.properties")
public class RabbitConfig {
    @Value("${com.edgardeng.queue.first}")
    private String firstQueue;
    @Value("${com.edgardeng.queue.second}")
    private String secondQueue;
    @Value("${com.edgardeng.queue.third}")
    private String thirdQueue;
    @Value("${com.edgardeng.queue.fourth}")
    private String fourthQueue;

    @Value("${com.edgardeng.exchange.topic}")
    private String topicExchange;
    @Value("${com.edgardeng.exchange.direct}")
    private String directExchange;
    @Value("${com.edgardeng.exchange.fanout}")
    private String fanoutExchange;

    /** 创建队列**/

    @Bean("vipFirstQueue")
    public Queue getFirstQueue() {
        return new Queue(firstQueue);
    }
    @Bean("vipSecondQueue")
    public Queue getSecondQueue() {
        return new Queue(secondQueue);
    }
    @Bean("vipThirdQueue")
    public Queue getThirdQueue() {
        return new Queue(thirdQueue);
    }
    @Bean("vipFourthQueue")
    public Queue getFourthQueue() {
        return new Queue(fourthQueue);
    }

    /** 创建交换机*/
    @Bean("vipDirectExchange")
    public DirectExchange getDirectExchange() {
        return new DirectExchange(directExchange);
    }
    @Bean("vipTopicExchange")
    public TopicExchange getTopicExchange() {
        return new TopicExchange(topicExchange);
    }
    @Bean("vipFanoutExchange")
    public FanoutExchange getFanoutExchange() {
        return new FanoutExchange(fanoutExchange);
    }

    /** 定义绑定关系*/

    @Bean
    public Binding bindFirst(@Qualifier("vipFirstQueue") Queue queue, @Qualifier("vipDirectExchange") DirectExchange exchange ) {
        return BindingBuilder.bind(queue).to(exchange).with("key.direct");
    }
    @Bean
    public Binding bindSecond(@Qualifier("vipSecondQueue") Queue queue, @Qualifier("vipTopicExchange") TopicExchange exchange ) {
        return BindingBuilder.bind(queue).to(exchange).with("key.topic.*");
    }

    @Bean
    public Binding bindThird(@Qualifier("vipThirdQueue") Queue queue, @Qualifier("vipFanoutExchange") FanoutExchange exchange ) {
        return BindingBuilder.bind(queue).to(exchange);
    }
    @Bean
    public Binding bindFourth(@Qualifier("vipFourthQueue") Queue queue, @Qualifier("vipFanoutExchange") FanoutExchange exchange ) {
        return BindingBuilder.bind(queue).to(exchange);
    }


    /**
     * 在消费端转换json消息 监听类需要添加 containerFactory 属性
     * @param connectionFactory
     * @return
     */
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory){
        //SimpleRabbitListenerContainerFactory发现消息中有content_type有text就会默认将其转换成string类型的
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        factory.setAutoStartup(true);
        return factory;
    }



}
